package net.microfigure.ljfshiro.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.microfigure.ljfshiro.mapper.PermissionMapper;
import net.microfigure.ljfshiro.mapper.RoleMapper;
import net.microfigure.ljfshiro.mapper.RolePermissionMapper;
import net.microfigure.ljfshiro.mapper.UserMapper;
import net.microfigure.ljfshiro.model.Permission;
import net.microfigure.ljfshiro.model.Role;
import net.microfigure.ljfshiro.model.RolePermission;
import net.microfigure.ljfshiro.model.User;
import net.microfigure.ljfshiro.service.RoleService;
import net.microfigure.ljfshiro.util.ResultUtil;
import net.microfigure.ljfshiro.util.UUIDUtil;
import net.microfigure.ljfshiro.vo.base.ResponseVo;
@Service
public class RoleServiceImpl implements RoleService  {
	@Autowired
	private RoleMapper roleMapper;
	@Autowired
	private PermissionMapper permissionMapper;
	@Autowired
	private RolePermissionMapper rolePermissionMapper;
	@Autowired
	private UserMapper userMapper;


	@Override
	public Set<String> findRoleByUserId(String userId) {
		// TODO Auto-generated method stub
		return roleMapper.findRoleByUserId(userId);
	}

	@Override
	public List<Role> selectRoles(Role role) {
		// TODO Auto-generated method stub
		return roleMapper.selectRoles(role);
	}

	@Override
	public int insert(Role role) {
		role.setRoleId(UUIDUtil.getUniqueIdByUUId());
		role.setStatus(1);
		role.setCreateTime(new Date());

		return roleMapper.insert(role);
	}

	@Override
	public int updateStatusBatch(List<String> roleIds, Integer status) {
		Map<String,Object> params = new HashMap<String,Object>(2);
		params.put("roleIds",roleIds);
		params.put("status",status);
		return roleMapper.updateStatusBatch(params);	
	}

	@Override
	public Role findById(Integer id) {
		Role role = new Role();
		role.setId(id);
		return  roleMapper.selectByPrimaryKey(role);
	}

	@Override
	public int updateByRoleId(Role role) {
        Map<String,Object> params  = new HashMap<>(3);
        params.put("name",role.getName());
        params.put("description",role.getDescription());
        params.put("role_id",role.getRoleId());
        return roleMapper.updateByRoleId(params);
	}

	
	@Override
	public List<Permission> findPermissionsByRoleId(String roleId) {
		// TODO Auto-generated method stub
		return permissionMapper.findByRoleId(roleId);
	}
	//根据角色id保存分配权限
	@Override
	public ResponseVo addAssignPermission(String roleId, List<String> permissionIdsList) {
		  try{
	            RolePermission rolePermission = new RolePermission();
	            rolePermission.setRoleId(roleId);
	            rolePermissionMapper.delete(rolePermission);
	            for(String permissionId : permissionIdsList){
	                rolePermission.setId(null);
	                rolePermission.setPermissionId(permissionId);
	                rolePermissionMapper.insert(rolePermission);
	            }
	            return ResultUtil.success("分配权限成功");
	        }catch(Exception e){
	            return ResultUtil.error("分配权限失败");
	        }
	}

	@Override
	public List<User> findByRoleId(String roleId) {
		// TODO Auto-generated method stub
		return userMapper.findByRoleId(roleId);
	}

}
